var elementsIdCounter = 0;

var listItemCustomClick = function(idElement){

	
				var wsCallback = function(data){
					
					var firstTime = true;
					var jsonArray = dojo.fromJson(data);
					
					var speenerWheelLabels = new Array();
					for(var i = 0; i<jsonArray.length; i++){
						speenerWheelLabels.push(jsonArray[i].value);
					}				
					
					if(dojo.byId("overlayDOM_"+idElement)){
						dijit.byId("overlayDOM_"+idElement).destroyRecursive(false);
						dijit.byId("spinnerWheelDOM_"+idElement).destroyRecursive(false);
						dojo.disconnect(connection);
						firstTime = false;
					} 
					
						var overlayDOM = dojo.create("div",{id: "overlayDOM_"+idElement});
						var headingDOM = dojo.create("h1",{});
						var toolbarButtonDOM = dojo.create("div",{});
						var spinnerWheelDOM = dojo.create("div",{id: "spinnerWheelDOM_"+idElement});
						var spinnerWheelSlotDOM = dojo.create("div", {id: "spinnerWheelSlotDOM_"+idElement});
						
						overlayDOM.appendChild(headingDOM);
						headingDOM.appendChild(toolbarButtonDOM);
						overlayDOM.appendChild(spinnerWheelDOM);
						spinnerWheelDOM.appendChild(spinnerWheelSlotDOM);
						dojo.body().appendChild(overlayDOM);
						
						var overlayWidget = new dojox.mobile.Overlay({},overlayDOM);
						var headingWidget = new dojox.mobile.Heading({label: "SpinWheel"},headingDOM);
						headingWidget.startup();
						
						var toolbarButtonWidget = new dojox.mobile.ToolBarButton({
							label: "Done",  
							onClick: function(){ 
								overlayWidget.hide();
								dijit.byId(idElement).rightTextNode.innerHTML = spinnerWheelSlotWidget.getValue();
								dojo.publish("loadWS/"+idElement, spinnerWheelSlotWidget.getValue());
							} 
						}, toolbarButtonDOM);
				
						toolbarButtonWidget.startup();
						
						var spinnerWheel = new dojox.mobile.SpinWheel({},spinnerWheelDOM);
						
						var spinnerWheelSlotWidget = new dojox.mobile.SpinWheelSlot({
							labels: speenerWheelLabels,
						    style: "text-align:center; width: 100%",
							}, spinnerWheelSlotDOM);					
	 
						var showSpeenerWheel = function(){
							dijit.byId("overlayDOM_"+idElement).show();
						};
						
						spinnerWheel.startup();
						
						if(firstTime){
							var connection = dijit.byId(idElement).connect(dijit.byId(idElement), "onClick", showSpeenerWheel);
						}
						
				};
				
				var wsURL = dojo.attr(idElement, "wsURL");
				
				if(dojo.attr(idElement, "wsSource") == ""){
					wsGet(wsURL, wsCallback);
				}else{
					dojo.subscribe("loadWS/"+dojo.attr(idElement, "wsSource"), this, function(data){wsGet(wsURL+"/"+data, wsCallback);} );
				}
			};
			
var buttonSendFormClick = function(idElement){

	var dataToPost = {};
	dojo.query('#'+dojo.attr(idElement, "formToSend")+' > li').forEach(function(node, index, arr){
		dataToPost[dijit.byId(node.id).label] = dijit.byId(node.id).rightTextNode.innerHTML;
	  });
	
	var transition = function(data){
		var w = dijit.byId("View1");
		w.performTransition('#View2',1,"slide",null);
		dojo.publish("newData/"+dojo.attr(idElement, "formToSend"), data);
	};
		
	wsPost( dojo.attr(idElement, "wsURL"), dataToPost, transition, {"Content-Type": "text/plain"});
	
};


var roundRectListReceiveData = function(idElement){
	
	var fillRoundRectDataList = function(data){
		var results = dojo.fromJson(data);
						
		dojo.query('#'+idElement + '> li').forEach(function(node, index, arr){
			dijit.byId(node.id).destroyRecursive();
		});
		
		for(var i=0; i<results.length; i++){
			var itemContainer = dojo.create("li", {id: "listItemDynamic" + elementsIdCounter});
		 		elementsIdCounter++;
		 		dojo.byId(idElement).appendChild(itemContainer);
		 		
		 		var listItem = new dojox.mobile.ListItem({ label: results[i].marca, rightText: results[i].modello, clickable: true, url: "http://192.168.0.100:8282/GUIEngine/rest/ws/auto/"+results[i].modello}, itemContainer.id);
		 		listItem.startup();
		}

	};
	
	dojo.subscribe("newData/"+dojo.attr(idElement, "formSource"), fillRoundRectDataList);
	
};